//
//  WXLAllBothListenCollectionViewCell.m
//  段子手
//
//  Created by dllo on 16/3/20.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "WXLAllBothListenCollectionViewCell.h"

@interface WXLAllBothListenCollectionViewCell ()

@property (nonatomic, retain) UILabel *dexLabel; /**< 标记label（1、2、3......） */
@property (nonatomic, retain) UIImageView *imgView; /**< 大图 */
@property (nonatomic, retain) UILabel *musicNameLabel; /**< 歌名 */
@property (nonatomic, retain) UILabel *musicSingerNameLabel; /**< 演唱者 */
//@property (nonatomic, retain) UIImageView *startImage; /**< 右边播放图标 */
@property (nonatomic, retain) UIButton *mVButton; /**< MV标记 */

@end

@implementation WXLAllBothListenCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

- (void)createSubView {
    
    self.dexLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, self.bounds.size.height)];
    [self addSubview:_dexLabel];
    _dexLabel.textAlignment = NSTextAlignmentCenter;
    _dexLabel.backgroundColor = [UIColor clearColor];
    _dexLabel.textColor = [UIColor lightGrayColor];
    
    self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 0, self.bounds.size.height, self.bounds.size.height)];
    [self addSubview:_imgView];
  
    self.musicNameLabel = [[UILabel alloc] init];
    [self addSubview:_musicNameLabel];
    
    self.musicSingerNameLabel = [[UILabel alloc] init];
    [self addSubview:_musicSingerNameLabel];
    _musicSingerNameLabel.font = [UIFont systemFontOfSize:15];
    _musicSingerNameLabel.textColor = [UIColor colorWithWhite:0.428 alpha:1.000];
    
//    self.startImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width - 50, (self.bounds.size.height - 30) / 2, 30, 30)];
//    [self addSubview:_startImage];
    
    self.mVButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [self addSubview:_mVButton];
    [_mVButton setTitle:@"▶︎MV" forState:(UIControlStateNormal)];
    [_mVButton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    _mVButton.titleLabel.font = [UIFont systemFontOfSize:10];
    [_mVButton addTarget:self action:@selector(mVButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
}

- (void)setModel:(WXLSongsModel *)model {
    _model = model;
    
    [_imgView HANDLEIMAGE(_model.picUrl)];
    _musicNameLabel.text = _model.name;
    _musicSingerNameLabel.text = [NSString stringWithFormat:@"%@ ♥︎%@", _model.singerName, _model.favorites];
//    _startImage.image = [UIImage imageNamed:@"iconfont-bofang"];
    
    CGRect rect = [WxlAPPTools scripeSuitWidthWithString:_model.name Font:17 Size:CGSizeMake(self.bounds.size.width - 100, self.bounds.size.height / 2)];
   
    if (rect.size.width > self.bounds.size.width - 150 && _model.mvListUrl.length) {
        _musicNameLabel.frame = CGRectMake(self.dexLabel.frame.size.width + self.imgView.frame.size.width + 10, 0, self.bounds.size.width - 250, self.bounds.size.height / 2);
    } else {
        _musicNameLabel.frame = CGRectMake(self.dexLabel.frame.size.width + self.imgView.frame.size.width + 10, 0, rect.size.width, self.bounds.size.height / 2);
    }
    
    _musicSingerNameLabel.frame = CGRectMake(_musicNameLabel.frame.origin.x, _musicNameLabel.frame.size.height, self.bounds.size.width - _imgView.frame.size.width - 5 - 100, _musicNameLabel.frame.size.height);
    
    // 调用该放法实现mVbutton是否显示
    [self createMvButton:rect];
    
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    _indexPath = indexPath;
    
    _dexLabel.text = [NSString stringWithFormat:@"%ld", _indexPath.row + 1];
}


- (void)createMvButton:(CGRect)rect {
    
    if (!_model.mvListUrl.length) {
        _mVButton.frame = CGRectMake(0, 0, 0, 0);
        return;
    }
    
    _mVButton.frame = CGRectMake(_musicNameLabel.frame.origin.x + _musicNameLabel.frame.size.width + 10, _musicNameLabel.frame.origin.y + (_musicNameLabel.frame.size.height - rect.size.height + 5) / 2, 40, rect.size.height - 5);
    _mVButton.layer.cornerRadius = _mVButton.frame.size.height / 2;
    _mVButton.layer.masksToBounds = YES;
    _mVButton.layer.borderColor = [UIColor blueColor].CGColor;
    _mVButton.layer.borderWidth = 0.5;
}


#pragma mark - 点击 button
- (void)mVButtonAction:(UIButton *)button {
    [self.delegate mVButtonAtionClick:_model];
}



- (void)layoutSubviews {
    [super layoutSubviews];
}

@end
